﻿using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;

namespace Hongsh.Pay.Client.Proxy.Http {
    // Token: 0x02000024 RID: 36
    public class HttpConnection : IDisposable {
        // Token: 0x17000062 RID: 98
        // (get) Token: 0x060000FF RID: 255 RVA: 0x00002AE9 File Offset: 0x00000CE9
        public string Id { get; }

        // Token: 0x17000063 RID: 99
        // (get) Token: 0x06000100 RID: 256 RVA: 0x00002AF1 File Offset: 0x00000CF1
        public IPEndPoint LocalEndPoint {
            get {
                return Socket.LocalEndPoint as IPEndPoint;
            }
        }

        // Token: 0x17000064 RID: 100
        // (get) Token: 0x06000101 RID: 257 RVA: 0x00002B03 File Offset: 0x00000D03
        public IPEndPoint RemoteEndPoint {
            get {
                return Socket.RemoteEndPoint as IPEndPoint;
            }
        }

        // Token: 0x17000065 RID: 101
        // (get) Token: 0x06000102 RID: 258 RVA: 0x00002B15 File Offset: 0x00000D15
        public Socket Socket { get; }

        // Token: 0x17000066 RID: 102
        // (get) Token: 0x06000103 RID: 259 RVA: 0x00002B1D File Offset: 0x00000D1D
        public Stream SocketStream { get; }

        // Token: 0x17000067 RID: 103
        // (get) Token: 0x06000104 RID: 260 RVA: 0x00002B25 File Offset: 0x00000D25
        public HttpServer Server { get; }

        // Token: 0x06000105 RID: 261 RVA: 0x0000B290 File Offset: 0x00009490
        internal HttpConnection(HttpServer server, Socket socket, Stream stream) {
            //Class15.cwcZ3SWztHroi();
            Id = Guid.NewGuid().ToString();
            InputStream = new MemoryStream();
            _InputBuffer = new byte[65535];
            //base..ctor();
            Server = server;
            Socket = socket;
            SocketStream = stream;
        }

        // Token: 0x06000106 RID: 262 RVA: 0x0000B2F4 File Offset: 0x000094F4
        public IAsyncResult Process() {
            Task task = new Task(delegate () {
                try {
                    InputStream.SetLength(0L);
                    HttpRequestHeader httpRequestHeader = null;
                    byte[] array = new byte[3];
                    int num = 5;
                    do {
                        int i = 0;
                        int num2 = SocketStream.Read(_InputBuffer, 0, _InputBuffer.Length);
                        if (num2 < 0) {
                            num = 0;
                        }
                        else if (num2 == 0) {
                            num--;
                            Thread.Sleep(50);
                        }
                        else {
                            num = 5;
                            if (httpRequestHeader == null) {
                                for (i = 0; i < num2; i++) {
                                    byte b = _InputBuffer[i];
                                    if (array[0] == 13 && array[1] == 10 && array[2] == 13 && b == 10) {
                                        i++;
                                        byte[] array2 = array;
                                        int num3 = 0;
                                        byte[] array3 = array;
                                        int num4 = 1;
                                        array[2] = 0;
                                        array3[num4] = 0;
                                        array2[num3] = 0;
                                        InputStream.Write(_InputBuffer, 0, i);
                                        InputStream.Seek(0L, SeekOrigin.Begin);
                                        httpRequestHeader = new HttpRequestHeader(new StreamReader(InputStream).ReadToEnd());
                                        InputStream.SetLength(0L);
                                        break;
                                    }
                                    array[0] = array[1];
                                    array[1] = array[2];
                                    array[2] = b;
                                    InputStream.WriteByte(b);
                                }
                            }
                            if (i != num2) {
                                InputStream.Write(_InputBuffer, i, num2 - i);
                            }
                        }
                        if (num <= 0) {
                            goto IL_178;
                        }
                    }
                    while (httpRequestHeader == null || InputStream.Length < httpRequestHeader.ContentLength);
                    HttpRequest req = new HttpRequest(this, httpRequestHeader, InputStream);
                    HttpContext ctx = new HttpContext(this, req, new HttpResponse(this));
                    InputStream.Seek(0L, SeekOrigin.Begin);
                    Server.OnHttpContext(ctx);
                    goto IL_1D8;
                IL_178:
                    throw new Exception("请求读取失败！");
                }
                catch (Exception e) {
                    Server.OnHttpException(e);
                }
            IL_1D8:
                Close();
            });
            task.Start();
            return task;
        }

        // Token: 0x06000107 RID: 263 RVA: 0x0000B31C File Offset: 0x0000951C
        public void Close() {
            try {
                Socket.Shutdown(SocketShutdown.Both);
            }
            catch {
            }
            try {
                Socket.Close();
            }
            catch {
            }
            Server.OnHttpConnectionClosed(this);
        }

        // Token: 0x06000108 RID: 264 RVA: 0x00002B2D File Offset: 0x00000D2D
        public void Dispose() {
            Close();
            Socket.Dispose();
        }

        // Token: 0x0400007B RID: 123
        private readonly MemoryStream InputStream;

        // Token: 0x0400007C RID: 124
        private readonly byte[] _InputBuffer;
    }
}
